package com.example.zkfx

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.base.mvvm.BaseActivity
import com.example.zkfx.adapter.GoodsAdapter
import com.example.zkfx.adapter.OneAdapter
import com.example.zkfx.adapter.TwoAdapter
import com.example.zkfx.databinding.ActivityCateBinding
import com.example.zkfx.room.AppDbUtils

class CateActivity : BaseActivity<ActivityCateBinding,CateVideModel>() {
    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(this)

        twoAdapter = TwoAdapter()
        binding.rvTwo.adapter = twoAdapter
        binding.rvTwo.layoutManager = GridLayoutManager(this,3)

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = LinearLayoutManager(this)

        //请求分类数据、、不请求商品
        //加载过后，从数据库中获取，不在请求请求后台
        val queryAll = AppDbUtils.getDb().getDao().queryAll()
        if(queryAll.size>0){
            //显示数据
        }else{
            viewModel.category()
        }


        //三级联动//点击一级，二级数据变化  三级数据也要变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.setId(oneAdapter.data[position].id)
            //二级数据变化
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data[position].child)
            twoAdapter.notifyDataSetChanged()

            //三级变化灭有点击二级，所以选择第一个
            viewModel.cateXq(twoAdapter.data[0].id,"",10,false,1,"","")
        }

        //点击二级，三级数据跟着变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.cateXq(twoAdapter.data[position].id,"",10,false,1,"","")
        }
    }

    override fun initData() {
        //观察分类数据，加载一级分类数据 加载二级分类数据  加载商品数据
        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()

                //二级分类数据  显示的是一级分类的第一个的child
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data[0].child)
                twoAdapter.notifyDataSetChanged()

                //加载对应商品  加载的是 二级分类中的第一个的cid
                viewModel.cateXq(twoAdapter.data[0].id,"",10,false,1,"","")

            }
        }
        viewModel.cateXqSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
                //加载成功，将数据进行存储
               /* it.data.list.forEach {
                    AppDbUtils.getDb().getDao().insert(it)
                }*/
            }
        }

    }

    override fun getViewModel(): Class<CateVideModel>  = CateVideModel::class.java

    override fun getLayout(): Int  = R.layout.activity_cate

}